﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using FTchina.Logic;
using PWMIS.DataProvider.Data;
using PWMIS.DataProvider.Adapter;

public partial class ui_BPM_Default : System.Web.UI.Page
{
    AdoHelper adohelper;//查询用助手
    protected void Page_Load(object sender, EventArgs e)
    {
        adohelper = MyDB.GetDBHelperByConnectionName("BPMDB");
        Get();
    }


    protected void Get()
    {
        string sql = "select x.*, u.user_name,u.user_mobile,u.user_email from flow07.usr_user u, " +
" (select USer_code, count( USer_code) as uernum   from " +
" (select processname,incident,steplabel,assignedtouser,view_usr_job_dep.USer_code,view_usr_job_dep.USER_NAME,view_usr_job_dep.dpt_name, starttime from flow07.tasks " +
" left join flow07.view_usr_job_dep on view_usr_job_dep.USER_ULT_CODE = tasks.assignedtouser  " +
"  where status = 1  and  processname like '%东营%'  " +
"  and trunc(sysdate - starttime)>30 " +
"  order by starttime desc) q   " +
"  group by USer_code " +
" )x " +
" where u.user_code=x.USer_code " +
"  order by x.uernum desc ";

        //Response.Write(sql);

        string sumsql =
" select sum(count( USer_code) )as uernum   from " +
" (select processname,incident,steplabel,assignedtouser,view_usr_job_dep.USer_code,view_usr_job_dep.USER_NAME,view_usr_job_dep.dpt_name, starttime from flow07.tasks " +
" left join flow07.view_usr_job_dep on view_usr_job_dep.USER_ULT_CODE = tasks.assignedtouser  " +
"  where status = 1  and  processname like '%东营%'  " +
"  and trunc(sysdate - starttime)>30 " +
"  order by starttime desc) q   " +
"  group by USer_code ";


        Grid1.PageSize = 20;
        DataTable dt = adohelper.ExecuteDataSet(sql).Tables[0];
        string single = PWMIS.Common.SQLPage.MakeSQLStringByPage(PWMIS.Common.DBMSType.Oracle, sql, "",Grid1.PageSize,Grid1.PageIndex+1,dt.Rows.Count);

       
        Grid1.RecordCount =dt.Rows.Count;
        Grid1.DataSource = adohelper.ExecuteDataSet(single).Tables[0];
        Grid1.DataBind();

        txbsum.Text = dt.Rows.Count.ToString() + "人" + adohelper.ExecuteScalar(sumsql).ToString() + "条";
 

    }

    protected void SendMessage(DataTable dt)
    {
        string error = string.Empty;
        string responseerror = string.Empty;
        string mess = "您账户{0}在流程管理系统中有{1}条流程超过30天未处理！请及时登录流程管理系统进行处理，释放占用的系统资源!";
        string from = "流程管理系统";
        foreach (DataRow dr in dt.Rows)
        {
            string target = dr["USER_MOBILE"].ToString();
            string messContent = string.Format(mess, dr["USER_CODE"].ToString(), dr["UERNUM"].ToString());
            FTchina.OA.UnicomSMS.SendSingle(target, from, messContent, out error);
            responseerror += error;
        }
        AlertLogic.ShowInfo(responseerror);
    }



    protected void btnok_Click(object sender, EventArgs e)
    {
        string sql = "select x.*,  u.user_mobile,u.user_email from flow07.usr_user u, " +
" (select USer_code, count( USer_code) as uernum   from " +
" (select processname,incident,steplabel,assignedtouser,view_usr_job_dep.USer_code,view_usr_job_dep.USER_NAME,view_usr_job_dep.dpt_name, starttime from flow07.tasks " +
" left join flow07.view_usr_job_dep on view_usr_job_dep.USER_ULT_CODE = tasks.assignedtouser  " +
"  where status = 1  and  processname like '%东营%'  " +
"  and trunc(sysdate - starttime)>30 " +
"  order by starttime desc) q   " +
"  group by USer_code " +
" )x " +
" where u.user_code=x.USer_code " +
"  order by x.uernum desc ";

        SendMessage(adohelper.ExecuteDataSet(sql).Tables[0]);
    }



    protected void btnmail_Click(object sender, EventArgs e)
    {
        string sql = "select x.*,  u.user_mobile,u.user_email from flow07.usr_user u, " +
" (select USer_code, count( USer_code) as uernum   from " +
" (select processname,incident,steplabel,assignedtouser,view_usr_job_dep.USer_code,view_usr_job_dep.USER_NAME,view_usr_job_dep.dpt_name, starttime from flow07.tasks " +
" left join flow07.view_usr_job_dep on view_usr_job_dep.USER_ULT_CODE = tasks.assignedtouser  " +
"  where status = 1  and  processname like '%东营%'  " +
"  and trunc(sysdate - starttime)>30 " +
"  order by starttime desc) q   " +
"  group by USer_code " +
" )x " +
" where u.user_code=x.USer_code " +
"  order by x.uernum desc ";

        SendEmail(adohelper.ExecuteDataSet(sql).Tables[0]);
    }

    private void SendEmail(DataTable dt)
    {
        foreach (DataRow dr in dt.Rows)
        {
            string titletmp = "您的账户{0}在流程管理系统中有{1}条流程超过30天未处理！请及时登录流程管理系统进行处理，释放占用的系统资源!";
            string title = string.Format(titletmp, dr["USER_CODE"].ToString(), dr["UERNUM"].ToString());
            string mess = "<h2 style='color:#FF0000'>" + title +
    " <br>" +
    " 请访问 门户首页-->我的应用--->流程管理操作手册 ，重点查看第19页关于取消实例的说明。 " +
    "<br>" +
    "</h2>";

           FTchina.OA.Email.Send(dr["USER_EMAIL"].ToString(), title, mess);
        }
    }



    /// <summary>
    /// 分页事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Grid1_PageIndexChange(object sender, FineUI.GridPageEventArgs e)
    {

        Grid1.PageIndex = e.NewPageIndex;
        Get();
    }
}